package cn.com.nes.site.service.iot.impl;

import cn.com.nes.common.em.MmEnum;
import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.com.nes.common.web.CommonResult;
import cn.com.nes.site.kafka.KafkaTemplate;
import cn.com.nes.site.service.iot.V2ProtocolService;
import cn.com.nes.site.service.iot.bo.OverloadCacheBO;
import cn.com.nes.site.service.system.ope.OpeService;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 设备协议服务
 *
 * @Author: ShenWei
 * @Date: 2024/09/20/14:08
 */
@Service
@Slf4j
public class V2ProtocolServiceImpl implements V2ProtocolService {

    @Resource
    private OpeService opeService;

    @Override
    public CommonResult<?> overloadCache(OverloadCacheBO bo) {
        if (StrUtil.isBlank(bo.getProtocolId())) {
            throw new MissBusinessParameterException("protocolId must not null");
        }
        try {
            // 发送协议号
            KafkaTemplate.getInstance().sendMm(bo.getProtocolId(), MmEnum.DeviceProtocolMm);
            // 发送重载指令
            opeService.reloadProtocolInfo(bo.getProtocolId());
            return CommonResult.ok("缓存重载成功!");
        } catch (Exception e) {
            log.info("缓存重载失败,失败原因:{}", e.getMessage());
        }
        return CommonResult.error("缓存重载失败!");
    }
}
